package com.haiyou.common.spring.component;

/**
 * 
 * 
 * @Description: 基于 @Component 生命周期控制
 * @author xingyuan
 * @date 2022年5月17日 下午8:30:27
 */
public interface LifecycleableComponent extends Comparable<LifecycleableComponent>{

	/**
	 * 服务器节点数据初始化
	 */
	public static final int server_node_init = -1;
	
	/**
	 * topic 初始化
	 */
	public static final int topic_init = 0;

	/**
	 *  accessor 等待 persister 执行完成
	 */
	public static final int persister_order = -3;
	
	/**
	 * accessor 执行
	 */
	public static final int accessor_order = -2;

	public static final int first_order = -9999;
	
	public static final int default_order = 1;

	public static final int static_order = 2;
	
	public static final int service_order = 3;
	
	public static final int after_service_order = 4;

	public static final int end = 9999;

	/**
	 * 重写来改变启动和停止顺序，值越小启动越早，销毁越晚，目前没有开放出来，待后续看需求，正常情况下基于各个模块相互隔离的原则应该不关注启动顺序
	 * 
	 * @return
	 */
	default int getOrder() {
		return default_order;
	}

	default void start() {
		try {
			init();
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage() + ":" + this, e);
		} finally {

		}
	}

	default void stop() {
		try {
			destory();
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		} finally {

		}
	}

	@Override
	default int compareTo(LifecycleableComponent o) {
		return Integer.compare(this.getOrder(), o.getOrder());
	}

	/**
	 * 初始化需要做点什么
	 */
	void init();

	/**
	 * 销毁前做点什么
	 */
	void destory();
	
}

